package org.enwey.icarenew.Activity.Login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.enwey.icarenew.API.APIManager;
import org.enwey.icarenew.API.RequestResult;
import org.enwey.icarenew.APPUtils.ToastUtil;
import org.enwey.icarenew.Activity.Base.BaseActivity;
import org.enwey.icarenew.Model.User;
import org.enwey.icarenew.R;

import java.util.Hashtable;


/**
 * Created by enwey on 2014/11/23.
 */
public class RegisterSetPassword extends BaseActivity {
    private EditText mVerifyCode;
    private EditText mPassword;
    private TextView mRegister;
    private TextView mGetVerifyCode;
    private String phoneNum;
    // 测试使用
    private boolean isSend = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_password);
        setUp();
        findView();
        registerListener();
    }

    private void setUp() {
        showLeftBtn();
        setTitle("设置密码");
        phoneNum = getIntent().getStringExtra("phone");
    }

    private void findView() {
        mVerifyCode = (EditText) this.findViewById(R.id.activity_register_password_identify);
        mGetVerifyCode = (TextView) this.findViewById(R.id.activity_register_password_identify_get);
        mPassword = (EditText) this.findViewById(R.id.activity_register_password_password);
        mRegister = (TextView) this.findViewById(R.id.activity_register_password_btn);
    }

    private void registerListener() {
        View.OnClickListener register = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.activity_register_password_identify_get:
                        getVerifyCode();
                        break;
                    case R.id.activity_register_password_btn:
                        doRegister();
                        break;
                    default:
                        break;
                }
            }
        };

        mRegister.setOnClickListener(register);
        mGetVerifyCode.setOnClickListener(register);
    }

    private void doRegister() {
        String verifyCode = mVerifyCode.getText().toString().trim();
        String password = mPassword.getText().toString().trim();

        if (TextUtils.isEmpty(verifyCode)) {
            ToastUtil.show(RegisterSetPassword.this, "验证码不能为空!");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.show(RegisterSetPassword.this, "密码不能为空!");
            return;
        }

//        if (verifyCode.length() <= 0 || verifyCode.length() >= 10) {
//            ToastUtil.show(RegisterSetPassword.this, "验证码错误...");
//            return;
//        }

        if (password.length() < 6 || password.length() > 16) {
            ToastUtil.show(RegisterSetPassword.this, "密码长度为6~16个字符!");
            return;
        }

        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtil.show(RegisterSetPassword.this, "手机号码不能为空！");
            return;
        }

        // 回调api注册方法
        CallAPIRegister(password);
    }

    private void CallAPIRegister(String password) {
        showProgressDialog().setCancelable(false);
        Hashtable parameters = new Hashtable();
        parameters.put("account", phoneNum);
        parameters.put("password", password);

        APIManager.getInstance(this).registerUser(parameters, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                hideProgressDialog();
                showException(error);
            }
        }, new Response.Listener<RequestResult<User>>() {
            @Override
            public void onResponse(RequestResult<User> response) {
                hideProgressDialog();
                if (hasError(response)) {
                    return;
                }

                if (response != null && response.message != null) {
                    ToastUtil.show(RegisterSetPassword.this, response.message);
                    goToLoginActivity(RegisterSetPassword.this);
                }
            }
        });
    }

    private void getVerifyCode() {
        isSend = !isSend;
        if (isSend) {
            mGetVerifyCode.setText("重新获取(60s)");
        }else {
            mGetVerifyCode.setText("获取验证码");
        }
    }

    private void goToLoginActivity(RegisterSetPassword weakActivity) {
        Intent intent = new Intent();
        intent.setClass(weakActivity, PerfectUserInfo.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("phone", phoneNum);
        startActivity(intent);
        finish();
    }
}
